package cn.edu.jlu.community.app.other;

//import android.app.Activity;
//import android.os.Bundle;
//import android.os.Handler;
//import android.os.Message;
//import android.view.View;
//import android.widget.TextView;
//
//import cn.edu.jlu.community.R;
//import cn.edu.jlu.community.sinvoice.LogHelper;
//import cn.edu.jlu.community.sinvoice.SinVoicePlayer;
//import cn.edu.jlu.community.sinvoice.SinVoiceRecognition;
//
//public class Unlock extends Activity implements SinVoiceRecognition.Listener, SinVoicePlayer.Listener{
//
//    private final static int MAX_NUMBER = 8;
//    private final static int MSG_SET_RECG_TEXT = 1;
//    private final static int MSG_RECG_START = 2;
//    private final static int MSG_RECG_END = 3;
//
//    private final static String CODEBOOK = "01234567";
//
//    private Handler mHanlder;
//    private SinVoicePlayer mSinVoicePlayer;
//    private SinVoiceRecognition mRecognition;
//
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_unlock);
//
//        mSinVoicePlayer = new SinVoicePlayer(CODEBOOK);
//        mSinVoicePlayer.setListener(this);
//
//    }
//
//
//    private String genText(int count) {
//        StringBuilder sb = new StringBuilder();
//        int pre = 0;
//        while (count > 0) {
//            int x = (int) (Math.random() * MAX_NUMBER + 1);
//            if (Math.abs(x - pre) > 0) {
//                sb.append(x);
//                --count;
//                pre = x;
//            }
//        }
//
//        return sb.toString();
//    }
//
//    private static class RegHandler extends Handler {
//        private StringBuilder mTextBuilder = new StringBuilder();
//        private TextView mRecognisedTextView;
//
//        public RegHandler(TextView textView) {
//            mRecognisedTextView = textView;
//        }
//
//        @Override
//        public void handleMessage(Message msg) {
//            switch (msg.what) {
//                case MSG_SET_RECG_TEXT:
//                    char ch = (char) msg.arg1;
//                    mTextBuilder.append(ch);
//                    if (null != mRecognisedTextView) {
//                        mRecognisedTextView.setText(mTextBuilder.toString());
//                    }
//                    break;
//
//                case MSG_RECG_START:
//                    mTextBuilder.delete(0, mTextBuilder.length());
//                    break;
//
//                case MSG_RECG_END:
//                    LogHelper.d(TAG, "recognition end");
//                    break;
//            }
//            super.handleMessage(msg);
//        }
//    }
//
//    @Override
//    public void onRecognitionStart() {
//        mHanlder.sendEmptyMessage(MSG_RECG_START);
//    }
//
//    @Override
//    public void onRecognition(char ch) {
//        mHanlder.sendMessage(mHanlder.obtainMessage(MSG_SET_RECG_TEXT, ch, 0));
//    }
//
//    @Override
//    public void onRecognitionEnd() {
//        mHanlder.sendEmptyMessage(MSG_RECG_END);
//    }
//
//    public void onClick(View view){
//
//    }
//}

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.avos.avoscloud.AVUser;

import cn.edu.jlu.community.R;
import cn.edu.jlu.community.app.base.ToolbarActivity;
import cn.edu.jlu.community.server.KeyGen;
import cn.edu.jlu.community.voice.LogHelper;
import cn.edu.jlu.community.voice.SinVoicePlayer;
import cn.edu.jlu.community.voice.SinVoiceRecognition;

public class UnlockActivity extends ToolbarActivity implements SinVoicePlayer.Listener {
    private final static String TAG = "MainActivity";
    private final static int MAX_NUMBER = 8;
    private final static int MSG_SET_RECG_TEXT = 1;
    private final static int MSG_RECG_START = 2;
    private final static int MSG_RECG_END = 3;

    private boolean isPlayed = false;

    private final static String CODEBOOK = "01234567";

    private SinVoicePlayer mSinVoicePlayer;
    private SinVoiceRecognition mRecognition;

    private AVUser user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mSinVoicePlayer = new SinVoicePlayer(CODEBOOK);
        mSinVoicePlayer.setListener(this);

        user = AVUser.getCurrentUser();

        View playStart = this.findViewById(R.id.unlock_button);
        final ImageView unlockImg = (ImageView)this.findViewById(R.id.unlock_img);
        final TextView unlockText = (TextView)this.findViewById(R.id.unlock_text);
        playStart.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                if (isPlayed) {
                    unlockImg.setImageResource(R.drawable.unlock_red);
                    unlockText.setText(R.string.unlock_tip);

                    mSinVoicePlayer.stop();
                    isPlayed = false;
                } else {
                    String text = user.getString("key");
                    String key = "";
                    try {
                        key = new String(KeyGen.run(text.toCharArray()));
                    } catch (Exception e) {
                        key = "12365467";
                    }

                    unlockImg.setImageResource(R.drawable.unlock_blue);
                    unlockText.setText(R.string.unlocking_tip);

                    mSinVoicePlayer.play(key, true, 1000);
                    isPlayed = true;
                }
            }
        });

    }

    @Override
    protected int getLayout() {
        return R.layout.unlock_activity;
    }

    @Override
    protected void onDestroy(){
        mSinVoicePlayer.stop();
        super.onDestroy();
    }

    private String genText(int count) {
        StringBuilder sb = new StringBuilder();
        int pre = 0;
        while (count > 0) {
            int x = (int) (Math.random() * MAX_NUMBER + 1);
            if (Math.abs(x - pre) > 0) {
                sb.append(x);
                --count;
                pre = x;
            }
        }

        return sb.toString();
    }

    @Override
    public void onPlayStart() {
        LogHelper.d(TAG, "start play");
    }

    @Override
    public void onPlayEnd() {
        LogHelper.d(TAG, "stop play");
    }

}
